package game

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ UserOpenId1Model = (*customUserOpenId1Model)(nil)

type (
	// UserOpenId1Model is an interface to be customized, add more methods here,
	// and implement the added methods in customUserOpenId1Model.
	UserOpenId1Model interface {
		userOpenId1Model
		FindLMaxUid(ctx context.Context) (int64, error)
	}

	customUserOpenId1Model struct {
		*defaultUserOpenId1Model
	}
)

// NewUserOpenId1Model returns a model for the database table.
func NewUserOpenId1Model(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) UserOpenId1Model {
	return &customUserOpenId1Model{
		defaultUserOpenId1Model: newUserOpenId1Model(conn, c, opts...),
	}
}

func (m *customUserOpenId1Model) FindLMaxUid(ctx context.Context) (int64, error) {
	query := fmt.Sprintf("select COALESCE(Max(open_id),1) from %s ", m.table)
	var uid int64
	err := m.QueryRowNoCacheCtx(ctx, &uid, query)
	switch err {
	case nil:
		return uid, nil
	default:
		return 0, err
	}
}
